var InterValObj; //timer变量，控制时间
var count = 60; //间隔函数，1秒执行
var curCount=0;//当前剩余秒数
var path = getContextPath();
//邮箱格式
function CheckMail(mail) { 
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
	if (filter.test(mail)){
		return true;
	}else{  
		return false;
	}
}
//电话格式
function CheckPhone(phone) {
	var fileter = /^1\d{10}$/;
	if (fileter.test(phone)){
		return true;
	}else{  
		return false;
	}
}

//QQ格式
function CheckQQCode(qqCode){
	var reg=/^\d{5,10}$/;  
	if (reg.test(qqCode)){
		return true;
	}else{  
		return false;
	}
}

//计时器
function SetRemainTime() {            
	if (curCount == 0) {                                
		window.clearInterval(InterValObj);//停止计时器                
		$("#sendPhoneCode").removeAttr("disabled");//启用按钮                
		$("#sendPhoneCode").html("重新发送验证码");                               
	}else{                
		curCount--;                
		$("#sendPhoneCode").html(curCount+"秒后重新发送");            
	}        
}
function getProvince(key, value) {
	$("#provinceName").attr("value",value);
	$("#provinceKey").attr("value",key);
}
function getExamYear(key) {
	$(".examYearMsg").hide();
	$("#examYear").attr("value",key);
}
function getPhoneProvince(key, value) {
	$("#phoneProvinceName").attr("value",value);
	$("#phoneProvinceKey").attr("value",key);
}
function getPhoneExamYear(key) {
	$(".examYearMsg").hide();
	$("#phoneExamYear").attr("value",key);
}
/**
 * 获初始的高考年份
 */
function getDefaultExamYear() {
	return $.cookie("examYear")?$.cookie("examYear"):getExamYearByGrade(getDefaultGradeKey());
}

/**
 * 获取初始的年级
 */
function getDefaultGradeKey() {
	return $.cookie("gradeKey")?$.cookie("gradeKey"):"seniorOne";
}
/**
 * 年级到年份的简单换算
 */
function getExamYearByGrade(grade) {
	var now = new Date();
	var year = now.getFullYear()*1;
	var month = now.getMonth()*1 +1;
	if("seniorOne"===grade) {
		if(month*1 >= 7) {
			return year*1 + 3;
		} 
		return year*1 + 2;
	} else if("seniorTwo"===grade) {
		if(month*1 >= 7) {
			return year*1 + 2;
		} 
		return year*1 + 1;
	} else {
		if(month*1 >= 7) {
			return year* + 1;
		} 
		return year*1;
	}
}



$(function(){
	$("i.check-box").click(function(){
		if($(this).hasClass('check-box-active')) {
			$(this).removeClass("check-box-active").addClass("check-box-default")
				.next().prop("checked", false);
		} else {
			$(this).removeClass("check-box-default").addClass("check-box-active")
				.next().prop("checked", true);
		}
	})
	
	$("#regditMail").blur(function(){
		var mail = $("#regditMail").val();
		if(mail == ''){
			$(this).next().html('邮箱不能为空').show();
		} else if(!CheckMail(mail)) {
			$(this).next().html('邮箱格式不正确').show();
		}
	});
	$("#mailUserName, #phoneUserName").blur(function(){
		var userName = $.trim($(this).val());
		var that = $(this);
		if(userName=='') {
			$(this).next().html("昵名不能为空").show();
		} else {
			$.post(path+"webAdmin/userNameUniqueCheck", {"userName": userName}, function(data) {
				if(!data) {
					that.next().html("此昵名已被他人使用").show();
				}
			});
		}
	});
	$("#mailUserName, #phoneUserName").focus(function() {
		$(this).next().hide();
	});
	
	$("#mailPwd").blur(function(){
		var pwd = $("#mailPwd").val().length;
		if(pwd < 6 || pwd >16){
			$(this).next().next().html("密码长度必须在6位到16位之间").show();
		}
	});
	$("#mailPwd").focus(function(){
		$(this).next().next().hide();
	});
	$("#regditMail").focus(function(){
		$(this).next().hide();
	});
	$("#showMailPwd").click(function(){
		if($("#mailPwd").attr("type").toUpperCase()=="PASSWORD") {
			$("#mailPwd").attr("type", "text");
		} else {
			$("#mailPwd").attr("type", "password");
		}
		showPwdEventCount();
	});
	$("#phone").blur(function(){
		var phone = $("#phone").val();
		if(phone == ''){
			$(this).next().html('手机号码不能为空').show();
		} else
		if(!CheckPhone(phone)){
			$(this).next().html('手机号码输入错误').show();
		}
	});
	$("#phone").focus(function(){
		$(this).next().hide();
	});
	$("#phonePwd").blur(function(){
		var pwd = $("#phonePwd").val().length;
		if(pwd < 6 || pwd >16){
			$("#phonePwdMsg").html("密码长度必须在6位到16位之间");
			$("#phonePwdMsg").show();
			return;
		}
	});
	$("#phonePwd").focus(function(){
		$(this).next().next().hide();
	});
	$("#showPhonePwd").click(function(){
		if($("#phonePwd").attr("type").toUpperCase=="PASSWORD") {
			$("#phonePwd").attr("type","text");
		} else {
			$("#phonePwd").attr("type","PASSWORD");
		}
		showPwdEventCount();
	});
	var flag=true;
	$("#sendPhoneCode").click(function(){
		
		if(!flag) return;
		var text = $("#sendPhoneCode").html();
		$(".btn,.account-btn").prop("disabled", true);
		var phone = $("#phone").val();
		if($.trim(phone)=="") {
			$("#codeMsg").html("手机号码不能为空").show();
			return;
		}
		
		
		if(text == '重新发送验证码' || text == '免费获取') {
			flag=false;
			$.ajax({
	    	    cache: true,
	    	    type: "POST",
	    	    dataType: "json",
	    	    url:path+"webAdmin/sendPhoneCode?phone="+phone,
	    	    data:$('#phoneForm').serialize(),
	    	    async: true,
	    	    error:function(XMLHttpRequest,textStatus,errorThrown){
	    	    	alert("网络异常！");
	    	    	flag=true;
	    	    },
	    	    success: function(data) {
	    	    	$(".btn,.account-btn").prop("disabled", false);
	    	    	if(data == 1){
	    	    		securityCodeEventCount();
	    	    		var vcurCount=60;
	    	    		$("#sendPhoneCode").html(vcurCount+"秒后重新发送");
	    	    		var timer = window.setInterval(function(){
	    	    			if (vcurCount == 0) {                                
	    	    				window.clearInterval(timer);//停止计时器         
	    	    				flag=true;
	    	    				$("#sendPhoneCode").removeAttr("disabled");//启用按钮                
	    	    				$("#sendPhoneCode").html("重新发送验证码");                               
	    	    			}else{
	    	    				flag=false;
	    	    				vcurCount--;                
	    	    				$("#sendPhoneCode").html(vcurCount+"秒后重新发送");            
	    	    			}  
	    	    		}, 1000); //启动计时器，1秒执行一次
	    	    		$("#sendPhoneCode").attr("disabled", "true"); 
	    	    	} else if (data== -1) {
	    	    		$("#codeMsg").html("该手机号码已被注册").show();
	    	    		flag=true;
	    	    	} else{
	    	    		$("#codeMsg").html("验证码发送失败！请查看手机号码是否输入正确").show();
	    	    		flag=true;
	    	    	}
	    		}
	    	});
		}else{
			return;
		}
	});
	$("#phoneCode").focus(function(){
		$(this).nextAll("span").hide();
	});
	$("#phoneCode").blur(function(){
		var code = $("#phoneCode").val();
		if(code == ''){
			$(this).nextAll("span").html("请输入验证码").show();
		}
	});
	
	$('#checkBoxDivEmail').click(function(){
		var s=$("#mail-check-box");
		if(s.prop('checked')==true){
		  s.prop('checked',false);
		}else{
		  s.prop('checked',true);
		}
		
		if(s.prop("checked")){
			$("#checkBoxDivEmail").addClass("checkedBox");
		}else{
			$("#checkBoxDivEmail").removeClass("checkedBox");
		}
	});	

	$('#checkBoxDivMb').click(function(){
		var s=$("#phone-check-box");
		if(s.prop('checked')==true){
		  s.prop('checked',false);
		}else{
		  s.prop('checked',true);
		}
		
		if(s.prop("checked")){
			$("#checkBoxDivMb").addClass("checkedBox");
		}else{
			$("#checkBoxDivMb").removeClass("checkedBox");
		}
	});		
	
	
	$("#signup1, #signup, #telSignup, #emailSignup").unbind('click').click(function(e) {
		var elId = $(this).attr("id");
		e.preventDefault();
		var type = $(this).attr("for");
		var form = $(this).closest("form");
		var checkboxStatus = $(".agreement", form);
		var userName = $.trim($("[name=userName]", form).val());
		var password =$.trim($("[name=loginPwd]", form).val());
		var loginName = $.trim($("[name=loginName]:enabled", form).val());
		var provinceKey = $.trim($("[name=provinceKey]", form).val());
		var provinceName = $.trim($("[name=provinceName]", form).val());
		var examYear = $.trim($("[name=examYear]", form).val());
		var phoneCode = $.trim($("#phoneCode").val());
		var phoneFlag = type=="email" || (type=="tel" && phoneCode!="");
		var url =path+ "webAdmin/regditIn?type="+type;
		if(type=="tel" && phoneCode!="") {
			url += "&code="+ phoneCode;
		}
			if(examYear=="") {
				$(".examYearMsg", form).text("请选择高考年份").show();
				return;
			}
			if(userName!="" && password!="" && loginName!="" && provinceKey!="" && provinceName!="" && phoneFlag   
				&& $(".phoneMsgWrong:visible", form).length==0 && $(".error:visible",form).length==0 ) {
				$.ajax({
		    	    cache: true,
		    	    type: "POST",
		    	    dataType: "json",
		    	    url:url,
		    	    data:form.serialize(),
		    	    async: true,
		    	    error:function(XMLHttpRequest,textStatus,errorThrown){
		    	    	alert("网络异常！");
		    	    },
		    	    success: function(data) {
		    	    	if (data == -1) {
		    	    		$("#codeMsg").html("验证码已失效，请重新获取验证码！");
		    				$("#codeMsg").show();
		    	    	} else if(data == 0) {
		    	    		$("#codeMsg").html("验证码输入错误！");
		    				$("#codeMsg").show();
		    	    	} else if(data == -2){
							swal("注册失败！", "该手机/邮箱已被注册！", "error");
						} else if(data == -4){
							swal("注册失败！", "该昵称已存在！", "error");
						} else if(data == -3) {
							swal("注册失败！", "账号注册失败！", "error");
						} else  {
							if(elId == 'signup1' || elId == 'signup'){
								submitRegistEventCount();
							}
		    	    		swal({  
	            	    		title: "恭喜您，注册成功！",
	            	    		text: type=="email"?"请及时查收邮件，点击邮件内链接完成邮箱验证！":"",  
	            	    		type: "success", 
	            	    		showCancelButton: false, 
	            	    		confirmButtonColor: "#DD6B55",  
	            	    		confirmButtonText: "确定", 
	            	    		cancelButtonText:"取消",
	            	    		closeOnConfirm: false,
	            	    		closeOnCancel: false
	            	    		}, 
	            	    		function(){
									$.cookie("provinceKey", provinceKey, {path: '/', expires: 1000});
									$.cookie("provinceName", provinceName, {path: '/', expires: 1000});
									$.cookie("examYear", examYear, {path: '/', expires: 1000});
									if(location.pathname.indexOf('regditIndp')!=-1) {
										location.href = getWeb()+"/index";
									} else {
										location.reload();
									}
	            	    		});
		    	    	}
		    		}
		    	});
			}
	});
});